#!/usr/bin/env bash
set -Eeo pipefail

USER="$(id -u)"

if [ -z "$(ls -A "/home/etcd/instance")" ]; then
    mkdir -p /home/etcd/instance/data
    cp /home/etcd/config.yaml.template /home/etcd/instance/config.yaml
fi

if [ "$USER" -eq 0 ]; then
    chown -R etcd:etcd /home/etcd/instance/
fi

exec gosu etcd "$@"
